import {
	defineConfig,
	loadEnv
} from "vite";
import path from 'path'
import uni from "@dcloudio/vite-plugin-uni";
import {
	UnifiedViteWeappTailwindcssPlugin as uvwt
} from "weapp-tailwindcss/vite";
import uniTailwind from '@uni-helper/vite-plugin-uni-tailwind';
import {
	resolve,
	WeappTailwindcssDisabled
} from "./shared"
import createVitePlugins from './vite/plugins'

/**
 * @type {import('vite').UserConfig}
 */
export default defineConfig(({
	mode,
	command
}) => {
	const env = loadEnv(mode, process.cwd());
	const alias = {
		// 设置路径
		'~': path.resolve(__dirname, './'),
		// 设置别名
		'@': path.resolve(__dirname, './src')
	}
	return {
		build: {
			sourcemap: env.NODE_ENV === 'development',
		},
		plugins: [
			uni(),
			uniTailwind(),
			uvwt({
				rem2rpx: true,
				disabled: WeappTailwindcssDisabled,
				tailwindcssBasedir: __dirname
			}),
		   createVitePlugins(env, command === 'build'),
		],
		define: {
			__VUE_I18N_FULL_INSTALL__: JSON.stringify(true),
			__VUE_I18N_LEGACY_API__: JSON.stringify(false),
			__INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
		},
		resolve: {
			// https://cn.vitejs.dev/config/#resolve-alias
			alias: alias
		},
		css: {
			postcss: {
				plugins: [
					require("tailwindcss")({
						config: resolve("./tailwind.config.js"),
					}),
					require("autoprefixer")(),
				],
			},
		},
		server: {
			proxy: {
				'/dev-api': {
					target: env.VITE_APP_API_HOST,
					changeOrigin: true,
					rewrite: (path) =>path.replace(/^\/dev-api/, '')
				},
				'/msghub': {
					target: env.VITE_APP_API_HOST,
					ws: true,
					rewrite: (path) => path.replace(/^\/msgHub/, '')
				}
			}
		}
	}
});